package com.igeek.javase.ch05.JDBC.dao;

import com.igeek.javase.ch05.JDBC.entity.Student;
import com.igeek.javase.ch05.JDBC.utils.JDBCUtilsByC3P0;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * @author chenmin
 * 2023/7/20 16:51
 * @description TODO
 */
public class SmsDao extends BaseDao<Student>{


    //统计学生人数
    public int selectCount() throws SQLException {
        String sql = "select count(*) from stus";
        long count = (long) this.selectSingleValue(JDBCUtilsByC3P0.getConn(), sql);
        return (int) count;
    }

    //查看学生名单
    public List<Student> findStudentAll() throws SQLException {
        String sql = "select * from stus";
        List<Student> students = this.selectAll(JDBCUtilsByC3P0.getConn(), sql, Student.class,null);
        return students;
    }

    //按学号查询学生姓名
    public Student findStudent(String id) throws SQLException {
        String sql = "select name,grade from stus where id = ?";
        Student student = this.selectOne(JDBCUtilsByC3P0.getConn(), sql, Student.class, id);
        return student;
    }


    //按姓名查询学生信息
    public List<Student> findStuByName(String name) throws SQLException {
        String sql = "select * from stus where name like concat('%',?,'%')";
        List<Student> students = this.selectAll(JDBCUtilsByC3P0.getConn(), sql, Student.class, name);
        return students;
    }

    //按学号修改学生出生日期
    public int updateStudent(String id, Date birthday) throws SQLException {
        String sql = "update stus set birthday = ? where id = ?";
        int i = this.update(JDBCUtilsByC3P0.getConn(), sql, birthday,id);
        return i;
    }


    //删除学生记录
    public int deleteStudent(String id) throws SQLException {
        String sql = "delete from stus where id = ?";
        int i = this.update(JDBCUtilsByC3P0.getConn(), sql, id);
        return i;
    }

}
